package com.lzx.applib.utils;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.lzx.applib.base.SafeHandler;

public class OddUtils {

	private OddUtils(){}
	
	public static void showInputMethod(final Activity activity, final View view) {
        showInputMethod(activity, view, 500);

		InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

	public static void showInputMethod(final Activity activity, final View view, int delay) {
		new SafeHandler(activity){
			public void handleMessage(android.os.Message msg) {
				InputMethodManager manager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
				manager.showSoftInput(view, 0);
				try {
					if (view instanceof EditText) {
						((EditText) view).setSelection(((EditText) view).getText().toString().length());
					}
				} catch (Exception e) {}
			};
		}.sendEmptyMessageDelayed(0, delay);
	}

	public static void setTextViewVisibleByText(TextView tv, String text) {
		if (TextUtils.isEmpty(text)) {
			tv.setVisibility(View.GONE);
		} else {
			tv.setVisibility(View.VISIBLE);
			tv.setText(text);
		}
	}

	public static int getCurrentAudio(Context context) {
		AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		return manager.getStreamVolume(AudioManager.STREAM_MUSIC);
	}

	public static int getMaxAudio(Context context) {
		AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		return manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
	}

	public static void setAudio(Context context, int audio) {
		AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		manager.setStreamVolume(AudioManager.STREAM_MUSIC, audio, 0);
	}

    public static void addAudio(Context context) {
        AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        manager.adjustVolume(AudioManager.ADJUST_RAISE, 0);
    }

    public static void minusAudio(Context context) {
        AudioManager manager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        manager.adjustVolume(AudioManager.ADJUST_LOWER, 0);
    }

	public static float getCurrentBrightness(Activity activity) {
		Window window = activity.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		return lp.screenBrightness;
	}

	public static void setBrightness(Activity activity, float brightness) {
		Window window = activity.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.screenBrightness = brightness;
		window.setAttributes(lp);
	}

	private static MediaPlayer mediaPlayer;
	private static boolean mediaPlaying = false;
	public static void playMusic(Context context, int resId) {
		if (mediaPlaying) return;

		if (mediaPlayer == null) {
			mediaPlayer = MediaPlayer.create(context, resId);
			mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
				@Override
				public void onCompletion(MediaPlayer mp) {
					mediaPlaying = false;
				}
			});
		}
		mediaPlayer.start();
		mediaPlaying = true;
	}
}
